package com.finger;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.widget.Toast;


public class FingerUtil {

	private final int VID = 0x2309;
	private final int PID = 0x0606;
	private UsbManager usbManager;
	public UsbDeviceConnection conn;
	private UsbInterface usbInterface;
	private UsbEndpoint inEndpoint;
	private UsbEndpoint outEndpoint;
	private Context mContext;
	private final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

	private volatile static FingerUtil singleton;
	private FingerUtil (){}
	public static FingerUtil getSingleton() {
		if (singleton == null) {
			synchronized (FingerUtil.class) {
				if (singleton == null) {
					singleton = new FingerUtil();
				}
			}
		}
		return singleton;
	}


	public void open(Context context) {
		mContext=context;
		close();
		
		usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

		for (final UsbDevice usbDevice : usbManager.getDeviceList().values()) {
			if (usbDevice != null && usbDevice.getProductId() == PID && usbDevice.getVendorId() == VID) {
				if (usbManager.hasPermission(usbDevice)) {
					openUsbDevice(usbDevice);
				} else {
					IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
					context.registerReceiver(mUsbPermissionActionReceiver, filter);

					PendingIntent mPermissionIntent = PendingIntent.getBroadcast(context, 0,
							new Intent(ACTION_USB_PERMISSION), 0);
					usbManager.requestPermission(usbDevice, mPermissionIntent);
				}
				break;
			}
		}
	}
	
	public boolean checkDev(Context context){
		usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);

		IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
		context.registerReceiver(mUsbPermissionActionReceiver, filter);

		boolean flag = false;
		for (final UsbDevice usbDevice : usbManager.getDeviceList().values()) {
			if (usbDevice != null && usbDevice.getProductId() == PID && usbDevice.getVendorId() == VID) {
				flag = true;
			}
		}
		return flag;
	}

	public void openUsbDevice(UsbDevice usbDevice) {
		this.conn = usbManager.openDevice(usbDevice);
		this.usbInterface = usbDevice.getInterface(0);
		this.inEndpoint = usbInterface.getEndpoint(0);
		this.outEndpoint = usbInterface.getEndpoint(1);
	}
	
	public int exeCmd(byte[] inByte, byte[] outByte){
		if(conn==null){
			return -1;
		}
		this.conn.bulkTransfer(outEndpoint, inByte, inByte.length, 5000);
		
		for(int i=0;i<30;i++){
			if(conn.controlTransfer(0xc0, 1, outByte.length, 0, outByte, 2, 5000)<2){
				try {
					Thread.sleep(200);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else{
				break;
			}
		}
		
		conn.bulkTransfer(inEndpoint, outByte, outByte.length, 5000);
		return 0;
	}
	
	public int getFd(){
		if (conn!=null){
			return conn.getFileDescriptor();
		}
		return -1;
	}
	
	public void close(){
		if(conn != null){
			boolean ret = conn.releaseInterface(usbInterface);
			/*while(!ret)
			{
				ret = conn.releaseInterface(usbInterface);
			}*/
			conn.close();
			conn = null;
		}
		try{
			mContext.unregisterReceiver(mUsbPermissionActionReceiver);
		}catch (Exception e){
		}

	}

	private final BroadcastReceiver mUsbPermissionActionReceiver = new BroadcastReceiver() {
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (ACTION_USB_PERMISSION.equals(action)) {
				synchronized (this) {
					UsbDevice usbDevice = (UsbDevice) intent
							.getParcelableExtra(UsbManager.EXTRA_DEVICE);
					if (intent.getBooleanExtra(
							UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
						if (null != usbDevice) {
							openUsbDevice(usbDevice);
						}
					} else {
						/*Toast.makeText(
								context,
								String.valueOf("Permission denied for device"
										+ usbDevice), Toast.LENGTH_LONG).show();*/
						/*Toast.makeText(
								context,
								String.valueOf("没有usb权限"), Toast.LENGTH_LONG).show();
*/
					}
				}
			}
		}
	};

}
